#include "space.h"

namespace roto
{ 
	namespace space
	{
		void negative(roPreImage* source)
		{
			uint height = source->height(), width = source->width();
			uchar* data = source->data();
			for(uint y = 0; y < height; y++)
				for(uint x = 0; x < width; x++)
				{
					long pos = (y * width + x) * 3;
					data[pos] = 255 - data[pos];
					data[pos + 1] = 255 - data[pos + 1];
					data[pos + 2] = 255 - data[pos + 2];
				}
		}

		void gradation(uchar* Gs, uint width, uint height, const int type, const double p, const double c)
		{
			switch(type)
			{
				case 6:
					for (uint x = 0; x < width; x ++)
						for (uint y = 0; y < height; y ++)
						{
							long xy = (y * width + x);
							Gs[xy] = 255 - Gs[xy];
						}
							break;
				
				case 7:
					for (uint x = 0; x < width; x ++)
						for (uint y = 0; y < height; y ++)
						{
							long xy = (y * width + x);
							double s = p * log2(1.0  + (double)Gs[xy]);
							if (s > 255)
								s = 255;
							Gs[xy] = (uint)s;
						}
						break;
					
				case 8:
					for (uint x = 0; x < width; x ++)
						for (uint y = 0; y < height; y ++)
						{
							long xy = (y * width + x);
							double s = c * pow((double)Gs[xy], p);
							if (s > 255)
								s = 255;
							Gs[xy] = (uint)s;
						}
						break;
				case 9:
					for (uint x = 0; x < width; x ++)
						for (uint y = 0; y < height; y ++)
						{
							long xy = (y * width + x);
							Gs[xy] = (Gs[xy] > p? 255 : 0);
						}
					break;
			}
		}
	}
}